<template>
  <!--项目执行信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :labelWidth="150"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBigMarketType } from '@/api/cms-api/projectOperating/plan/index';
import { formatdateByDay, formatdateByTime } from '@/utils/index';

export default {
  mixins: [dictionary],
  // model: {
  //   prop: 'pdata',
  //   event: 'click'
  // },
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      formValue: {},
      bigType: [] //存储项目大类集合
    };
  },
  mounted() {
    // console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
    //初始化项目大类
    this.initBigMarketType();
  },
  methods: {
    //取项目大类
    async initBigMarketType() {
      let rd = await getBigMarketType();
      this.bigType = rd.data; //将大类集合存储在变量中
                //页面必选字段控制
                if(this.value.row.projectMainClass==3){//广宣
                    this.initForm.find(item => item.fieldName === 'realRetentionNum').rules[0].required = false;//实际留档
                    this.initForm.find(item => item.fieldName === 'realOrderNum').rules[0].required = false;//实际订单
                    this.initForm.find(item => item.fieldName === 'realBoothAmount').rules[0].required = false;//展位批准金额 
                    this.initForm.find(item => item.fieldName === 'realBuildAmount').rules[0].required = false;//搭建运营批准金额 
                }else  if(this.value.row.projectMainClass==4){//活动  .rules[0].required = false
                    this.initForm.find(item => item.fieldName === 'realRetentionNum').rules[0].required = true;//实际留档
                    this.initForm.find(item => item.fieldName === 'realOrderNum').rules[0].required = true;//实际订单
                    this.initForm.find(item => item.fieldName === 'realBoothAmount').rules[0].required = false;//展位批准金额 
                    this.initForm.find(item => item.fieldName === 'realBuildAmount').rules[0].required = false;//搭建运营批准金额 
                }else{//CDE级车展
                    this.initForm.find(item => item.fieldName === 'realRetentionNum').rules[0].required = true;//实际留档
                    this.initForm.find(item => item.fieldName === 'realOrderNum').rules[0].required = true;//实际订单
                    this.initForm.find(item => item.fieldName === 'realBoothAmount').rules[0].required = false;//展位批准金额 
                    this.initForm.find(item => item.fieldName === 'realBuildAmount').rules[0].required = false;//搭建运营批准金额               
                }
    },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    init() {
         this.initForm.forEach(x => {
          x.initialValue = this.value.row[x.fieldName];
          if (x.fieldName == 'realStartDate') {
            x.initialValue = formatdateByDay(this.value.row.realStartDate);
          }
          if (x.fieldName == 'realEndDate') {
            x.initialValue = formatdateByDay(this.value.row.realEndDate);
          }
          if (x.fieldName == 'realLaunchReleaseTime') {
            if (this.value.row.realLaunchReleaseTime != null && this.value.row.realLaunchReleaseTime != '') {
              let releaseTime = this.value.row.realLaunchReleaseTime.split(',');
              x.initialValue = releaseTime;
            }
          }

           
          if (x.fieldName == 'realBuildAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
          if (x.fieldName == 'realBoothAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
          if (x.fieldName == 'planProjectAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
          if (x.fieldName == 'planApproveAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
          if (x.fieldName == 'realProjectAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
          if (x.fieldName == 'realApproveAmount') {
            if (x.initialValue == null) {
              x.initialValue = '';
            }
          }
        });
        // this.initForm.find(item => item.fieldName === 'orderCompletionRate').initialValue = this.value.row.orderCompletionRate / 100;
        // this.initForm.find(item => item.fieldName === 'guestsCompletionRate').initialValue = this.value.row.guestsCompletionRate / 100;
        // this.initForm.find(item => item.fieldName === 'retentionCompletionRate').initialValue = this.value.row.retentionCompletionRate / 100;
     
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'DATES',
          label: '实际开展/投放时间',
          fieldName: 'realLaunchReleaseTime',
          initialValue: null,
          placeholder: '',
          // dateType: 'date',
          valueFormat: 'yyyy-MM-dd',
          selfCols: 2,
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          change: value => {
            this.initReleaseTimeSmall(value);
          } 
        },
        {
          type: 'DATE',
          label: '实际开始日期',
          fieldName: 'realStartDate',
          valueFormat: 'yyyy-MM-dd',
          initialValue: '',
          selfCols: 1,
          disabled: true
          // rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '实际结束日期',
          fieldName: 'realEndDate',
          valueFormat: 'yyyy-MM-dd',
          selfCols: 1,
          initialValue: '',
          disabled: true
          // rules: [{ required: true, message: '请输入实际结束日期', trigger: 'blur' }]
        },
         {
          type: 'INPUT_NUMBER',
          label: '实际支持天数',
          fieldName: 'realTimeDay',
          initialValue: '',
          precision: 0,
          max: 99999999,
          disabled: true 
        },
        {
          type: 'INPUT',
          label: '实际项目地点',
          fieldName: 'realProjectLocation',
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          initialValue: '' 
        },
        {
          type: 'INPUT_NUMBER',
          label: '实际集客',
          fieldName: 'realGuestsNum',
          initialValue: null,
          placeholder:'数字',
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          max: 99999999,
          disabled: false ,
          change: this.guestsCalcChange
        },
       
        { type: 'INPUT', label: '搭建运营批准金额', fieldName: 'realBuildAmount', placeholder: '单位（元）', initialValue: undefined,numberFormat:true,
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '实际留档',
          fieldName: 'realRetentionNum',
          initialValue: undefined,
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          placeholder:'数字',
          max: 99999999,
          disabled: false,
          change: this.retentionCalcChange
        },
        { type: 'INPUT', label: '展位批准金额', fieldName: 'realBoothAmount',  placeholder: '单位（元）', initialValue: undefined,numberFormat:true,
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '实际订单',
          fieldName: 'realOrderNum',
          initialValue: undefined,
          placeholder:'数字',
          max: 99999999,
          rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          disabled: false,
          change: this.orderCalcChange
        },       
        {
          type: 'INPUT',
          label: '实际项目金额',
          fieldName: 'realProjectAmount',
          initialValue: '',
          numberFormat: true,
          rules: [{ required: true, message: '请输入实际项目金额', trigger: 'blur' }, { pattern: /[\d\.]+/g, message: '请输入数字', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '实际批准金额',
          fieldName: 'realApproveAmount',
          initialValue: '',
          disabled: true,
          placeholder: '',
          // rules: [{ required: true, message: '请输入实际开始日期', trigger: 'blur' }],
          numberFormat: true
        }
        
        // {
        //   type: 'INPUT',
        //   label: '订单完成率',
        //   fieldName: 'orderCompletionRate',
        //   initialValue: '',
        //   disabled: true,
        //   unitRender: () => {
        //     return <span slot="append">%</span>;
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '集客完成率',
        //   disabled: true,
        //   fieldName: 'guestsCompletionRate',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <span slot="append">%</span>;
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '留档完成率',
        //   disabled: true,
        //   fieldName: 'retentionCompletionRate',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <span slot="append">%</span>;
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '计划项目金额',
        //   disabled: true,
        //   fieldName: 'planProjectAmount',
        //   initialValue: '',
        //   placeholder: '',
        //   numberFormat: true
        // },
        // {
        //   type: 'INPUT',
        //   label: '计划批准金额',
        //   disabled: true,
        //   fieldName: 'planApproveAmount',
        //   initialValue: '',
        //   placeholder: '',
        //   numberFormat: true
        // },
    
 
       
        // {
        //   type: 'TEXT_AREA',
        //   label: '实际项目地点/投放平台',
        //   fieldName: 'realProjectLocation',
        //   initialValue: '',
        //   rules: [{ required: true, message: '请输入实际项目地点/投放平台', trigger: 'blur' }]
        // },
        // {
        //   type: 'TEXT_AREA',
        //   label: '项目执行描述',
        //   fieldName: 'projectDesc',
        //   initialValue: '',
        //   rules: [{ required: true, message: '请输入项目执行描述', trigger: 'blur' }]
        // },
        // {
        //   type: 'TEXT_AREA',
        //   label: '经销商自评',
        //   fieldName: 'dealerSelfEvalution',
        //   initialValue: '',
        //   selfCols: 2,
        //   placeholder: 'KPI达成情况、亮点、问题点及改进措施',
        //   rules: [{ required: true, message: '请输入经销商自评', trigger: 'blur' }],
        //   rows: 3
        // }
      ];
    },



   
 
      //是否需要变更报备
    isAllChange(val) {
      if (val === 10041001) {
        this.initForm.find(item => item.fieldName === 'planDesc').rules[0].required = true;
        this.initForm.find(item => item.fieldName === 'reportUrl').rules[0].required = true;
      } else {
        this.initForm.find(item => item.fieldName === 'planDesc').rules[0].required = false;
        this.initForm.find(item => item.fieldName === 'reportUrl').rules[0].required = false;
        this.$refs.formPanel.$refs.form.clearValidate();
      }
    },
    //集客完成率
    guestsCalcChange(val) {
      let guestsCompletionRate = ((val * 100) / this.value.row.targetGuestsNum).toFixed(2);
      if (!isNaN(guestsCompletionRate)) {
        this.initForm.find(item => item.fieldName === 'guestsCompletionRate').initialValue = parseFloat(guestsCompletionRate);
      }
    },
    //留档完成率
    retentionCalcChange(val) {
      let retentionCompletionRate = ((val * 100) / this.value.row.targetRetentionNum).toFixed(2);
      if (!isNaN(retentionCompletionRate)) {
        this.initForm.find(item => item.fieldName === 'retentionCompletionRate').initialValue = parseFloat(retentionCompletionRate);
      }
    },
    //实际开展/投放时间 改变方法
    initReleaseTimeSmall(val) {
      //清空 计划支持场次/投放天数、开始日期、结束日期
      this.initForm.find(item => item.fieldName === 'realTimeDay').initialValue = 0;
      this.initForm.find(item => item.fieldName === 'realStartDate').initialValue = '';
      this.initForm.find(item => item.fieldName === 'realEndDate').initialValue = '';
      // this.initForm.find(item => item.fieldName === 'projectFile').initialValue = vlue
      if (val != null && val.length > 0) {
        //如果计划开展/投放时间不为空
        //设置计划支持场次/投放天数为所选天的个数
        this.initForm.find(item => item.fieldName === 'realTimeDay').initialValue = val.length;
        let realStartDateTmp = '';
        let realEndDateTmp = '';
        //冒泡获取开始日期和结束日期
        for (let i = 0; i < val.length; i++) {
          if (!realStartDateTmp) {
            realStartDateTmp = val[i];
            realEndDateTmp = val[i];
          } else {
            if (realStartDateTmp > val[i]) {
              realStartDateTmp = val[i];
            }
            if (realEndDateTmp < val[i]) {
              realEndDateTmp = val[i];
            }
          }
        }
        this.initForm.find(item => item.fieldName === 'realStartDate').initialValue = formatdateByTime(realStartDateTmp);
        this.initForm.find(item => item.fieldName === 'realEndDate').initialValue = formatdateByTime(realEndDateTmp);
      }
    },
    formChangeHandle(val) {
      this.formValue = val;
      if (formatdateByDay(this.formValue.realStartDate) > formatdateByDay(this.formValue.realEndDate)) {
        this.$notify.warning({ title: '提示信息', message: '实际开始日期不能大于实际结束日期!' });
        return false;
      }
      if (this.formValue.realProjectAmount == 0 || this.formValue.realProjectAmount == null) {
        this.$notify.warning({ title: '提示信息', message: '实际项目金额必填!' });
        return;
      }
      // this.pdata = Object.assign(this.pdata, val);
      // //传递数据到父组件里
      //this.$emit('click', this.formValue);
      // //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.formValue);
    }
  },
  created() {
    this.init();
  }
};
</script>
